import java.util.ArrayList;
import java.util.List;

/**
 * 哈希表-快乐数-力扣202
 */
public class Practice_19 {
    public static void main(String[] args) {
        int n = 2;
        Boolean k = isHappy(n);
    }
    public static Boolean isHappy(int n) {
        List<Integer> list = new ArrayList<>();
        return func(list, n);
    }

    public static Boolean func(List<Integer> list, int n) {
        if (list.contains(n)) {
            return false;
        } else {
            list.add(n);
        }
        Integer m = n;
        String str = m.toString();
        char[] arraya = str.toCharArray();

        n = 0;
        for (int i = 0; i < arraya.length; i++) {
            int a = Integer.valueOf(String.valueOf(arraya[i]));
            n = n + a * a;
        }
        if (n == 1) {
            return true;
        } else if (n >= 1) {
            return func(list, n);
        } else {
            return false;
        }
    }
}
